package com.dabai.videobrowser.utils;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Description : Other
 *
 * @author BAI
 */
public class MediaFileUtils {

    /**
     * 把媒体文件的时间
     *
     * @param path 路径
     * @return int
     * @throws IOException ioexception
     */
    public static int getMediaFileTime(String path){
        try {
            MediaPlayer player = new MediaPlayer();
            player.setDataSource(path);
            player.prepare();
            int dur = player.getDuration();//获取音频的时间
            player.release();//记得释放资源
            return dur;
        } catch (IOException e) {
            return 0;
        }
    }

    /**
     * 把媒体文件的时间
     *
     * @param context 上下文
     * @param uri     uri
     * @return int
     * @throws IOException ioexception
     */
    public static int getMediaFileTime(Context context, Uri uri){
        try {
            MediaPlayer player = new MediaPlayer();
            player.setDataSource(context,uri);
            player.prepare();
            int dur = player.getDuration();//获取音频的时间
            player.release();//记得释放资源
            return dur;
        } catch (IOException e) {
            return 0;
        }
    }

    /**
     * 毫秒转 时间字符串
     *
     * @param millisecond 毫秒
     * @return {@link String}
     */
    public static String millisecondToTimeString(long millisecond){
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);
            dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));
            return dateFormat.format(new Date(millisecond));
        } catch (Exception e) {
            return "";
        }
    }


}
